﻿using System;
using System.Collections.Generic;

namespace AppMain
{
    public enum Action_Status
    {
        Wait,
        Running,
        Finish,
    }

    public class BaseAction
    {
        public int uid;
        public int id;
        protected IEvent[] events;
        public ActionRole role;
        public DamageData damageData;
        private float durTime;//经过的时间
        public float totalTime;//总时间
        private float currProgress;//当前进度
        private Action_Status Status = Action_Status.Wait;

        public BaseAction(IEvent[] events)
        {
            this.events = events;
        }

        public void enter(ActionRole role)
        {
            this.role = role;
            for (int i = 0; i < events.Length; i++)
            {
                events[i].Action = this;
                events[i].enter();
            }
        }

        public void tick(float frameTime)
        {
            if (this.Status == Action_Status.Finish) return;
            frameTime *= this.role.AttrMap.getAttr<float>(RoleAttr.AttackSpeed).Value;
            durTime += frameTime;
            currProgress = durTime / totalTime;
            //处理事件
            processEvents();
            if (currProgress >= 1)
            {
                //移除action
                this.Status = Action_Status.Finish;
            }
        }

        private void processEvents()
        {
            for (int i = 0; i < events.Length; i++)
            {
                IEvent baseEvent = events[i];
                if (currProgress >= baseEvent.Progress)
                {
                    baseEvent.processEvent();
                }
            }
        }

        public void exit()
        {
            for (int i = 0; i < events.Length; i++)
            {
                events[i].exit();
            }
        }
        public bool canRemove()
        {
            return this.Status == Action_Status.Finish;
        }

        public void dispose()
        {

        }


    }

}
